package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.TxnSafMsgMapper;
import cc.rengu.oltp.service.common.entity.TxnSafMsg;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 存储转发信息表操作接口实现类
 * Created by 王成 on 2020/2/6
 */
public class TxnSafMsgMapperImpl implements TxnSafMsgMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public TxnSafMsgMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertTxnSafMsg(TxnSafMsg txnSafMsg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertTxnSafMsg");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnSafMsg, null);
        if (0 != iReturnCode) {
            rglog.error("登记业务报文信息失败,instId:<{}>,sysSeqNum:<{}>,txnNum:<{}>,bizType:<{}>,iReturnCode:<{}>",
                    txnSafMsg.getInstId(), txnSafMsg.getSysSeqNum(), txnSafMsg.getTxnNum(), txnSafMsg.getBizType(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public TxnSafMsg selectTxnSafMsgByPrimaryKey(String instId, String sysSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTxnSafMsgByPrimaryKey");
        TxnSafMsg txnSafMsg = new TxnSafMsg();
        txnSafMsg.setInstId(instId);
        txnSafMsg.setSysSeqNum(sysSeqNum);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnSafMsg, txnSafMsg);
        if (0 != iReturnCode) {
            rglog.warn("根据主键查询业务报文信息失败,instId:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>", instId, sysSeqNum, iReturnCode);
            return null;
        }
        return txnSafMsg;
    }

    @Override
    public int updateTxnSafMsgByPrimaryKey(TxnSafMsg txnSafMsg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateTxnSafMsgByPrimaryKey");
        txnSafMsg.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnSafMsg, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新业务报文信息失败,instId:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>", txnSafMsg.getInstId(), txnSafMsg.getSysSeqNum(), iReturnCode);
        }
        return iReturnCode;
    }
}
